package com.tledu.publisher.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SendServiceAspect {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    //在向交换机发送消息,将发送过的消息后放入redis
    @After("execution(* com.tledu.publisher.service.SendService.send(..))")
    public void afterSend(JoinPoint joinPoint) {
        System.out.println("发送后aop");
        // 获取所有参数
        Object[] args = joinPoint.getArgs();
            // 获取第二个参数
            String secondArgument = (String) args[1];
        System.out.println("第二个参数为:"+secondArgument);
            //去掉后五位
            String modifiedArgument = secondArgument.substring(0, Math.max(0, secondArgument.length() - 5));
                //将得到的参数以逗号分割放入字符串数组中
                String[] parts = modifiedArgument.split(",");
                // 将parts中的每一个字符串作为key值放入Redis
                for (String part : parts) {
                    redisTemplate.opsForValue().setIfAbsent(part, "Warning occurred");
                    System.out.println("放入key: " + part);
                }
    }
}
    